﻿using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using MvcFlow.Mvc;

namespace MvcFlow
{
    public abstract class WorkflowController : AsyncController
    {
        private readonly IWorkflowRuntime workflowRuntime;

        protected async Task<ActionResult> OnExecuteWorkflowStep(string workflowName = null)
        {
            workflowName        = string.IsNullOrWhiteSpace(workflowName)? RouteData.GetRequiredString("controller") : workflowName;
            var actionResult    = await workflowRuntime.ExecuteStep(Request.RequestContext, workflowName);

            return actionResult != null ? actionResult.Bind(this) : new HttpStatusCodeResult(HttpStatusCode.InternalServerError,"Execution of workflow " + workflowName + " failed");
        }

        protected WorkflowController( IWorkflowRuntime workflowRuntime )
        {
            this.workflowRuntime = workflowRuntime;
        }
    }
}